1. 参考网址:https://forum.openmediavault....
  2. 创建应用GUI
    创建应用目录:/var/www/openmediavault/js/omv/module/admin/service/example
    创建菜单节点: Node.js

    // require("js/omv/WorkspaceManager.js")
    OMV.WorkspaceManager.registerNode({
        id: 'example',
        path: '/service',
        text: _('Example'),
        icon16: 'images/example.png',
        iconSvg: 'images/example.svg'
    });

    设置菜单节点图标
    var/www/openmediavault/images 内创建对应Node.js内的2张图片

    创建设置面板: Settings.js

    // require("js/omv/WorkspaceManager.js")
    // require("js/omv/workspace/form/Panel.js")
    Ext.define('OMV.module.admin.service.example.Settings', {
        extend: 'OMV.workspace.form.Panel',
        
        rpcService: 'Example',
        rpcGetMethod: 'getSettings',
        rpcSetMethod: 'setSettings',
        
        getFormItems: function() {
            return [{
                xtype: 'fieldset',
                title: _('General'),
                fieldDefaults: {
                    labelSeparator: ''
                },
                items: [{
                    xtype: 'checkbox',
                    name: 'enable',
                    fieldLabel: _('Enable'),
                    checked: false
                },
                {
                    xtype: 'numberfield',
                    name: 'max_value',
                    fieldLabel: _('Max value'),
                    minValue: 0,
                    maxValue: 100,
                    allowDecimals: false,
                    allowBlank: true
                }]
            }];
        }
    });
    
    OMV.WorkspaceManager.registerPanel({
        id: 'settings',
        path: '/service/example',
        text: _('Settings'),
        position: 10,
        className: 'OMV.module.admin.service.example.Settings'
    });

    刷新js缓存:

    source /usr/share/openmediavault/scripts/helper-functions && omv_purge_internal_cache
  3. 创建shell脚本
    生成配置信息的脚本postinst 命令执行:/bin/sh postinst configure

    #!/bin/sh
    
    set -e
    
    . /etc/default/openmediavault
    . /usr/share/openmediavault/scripts/helper-functions
    
    case "$1" in
        configure)
            SERVICE_XPATH_NAME="example"
            SERVICE_XPATH="/config/services/${SERVICE_XPATH_NAME}"
    
            # 添加默认配置
            if ! omv_config_exists "${SERVICE_XPATH}"; then
                omv_config_add_element "/config/services" "${SERVICE_XPATH_NAME}"
                omv_config_add_element "${SERVICE_XPATH}" "enable" "0"
                omv_config_add_element "${SERVICE_XPATH}" "max_value" "0"
            fi
    
            # 以下2条命令用于安装包安装  直接执行可注释掉
            dpkg-trigger update-fixperms
            dpkg-trigger update-locale
        ;;
    
        abort-upgrade|abort-remove|abort-deconfigure)
        ;;
    
        *)
            echo "postinst called with unknown argument" >&2
            exit 1
        ;;
    esac
    
    #DEBHELPER#
    
    exit 0 

    创建删除配置信息的shell脚本 postrm 执行命令:/bin/sh postrm purge

    #!/bin/sh
    
    set -e
    
    . /etc/default/openmediavault
    . /usr/share/openmediavault/scripts/helper-functions
    
    SERVICE_XPATH_NAME="example"
    SERVICE_XPATH="/config/services/${SERVICE_XPATH_NAME}"
    
    case "$1" in
        purge)
            if omv_config_exists ${SERVICE_XPATH}; then
                omv_config_delete ${SERVICE_XPATH}
            fi
        ;;
    
        remove)
        ;;
    
        upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
        ;;
    
        *)
            echo "postrm called with unknown argument \\`$1'" >&2
            exit 1
        ;;
    esac
    
    #DEBHELPER#
    
    exit 0
  4. 创建rpc
    在目录/usr/share/openmediavault/engined/rpc创建example.inc

    <?php
    class OMVRpcServiceExample extends \OMV\Rpc\ServiceAbstract {
    
        public function getName() {
            return "EXAMPLE";
        }
    
        public function initialize() {
            $this->registerMethod("getSettings");
            $this->registerMethod("setSettings");
        }
    
        public function getSettings($params, $context) {
            // Validate the RPC caller context.
            $this->validateMethodContext($context, [
                "role" => OMV_ROLE_ADMINISTRATOR
            ]);
            // Get the configuration object.
            $db = \\OMV\\Config\\Database::getInstance();
            $object = $db->get("conf.service.example");
            // Remove useless properties from the object.
            return $object->getAssoc();
        }
    
        public function setSettings($params, $context) {
            // Validate the RPC caller context.
            $this->validateMethodContext($context, [
                "role" => OMV_ROLE_ADMINISTRATOR
            ]);
            // Validate the parameters of the RPC service method.
            $this->validateMethodParams($params, "rpc.example.setsettings");
            // Get the existing configuration object.
            $db = \\OMV\\Config\\Database::getInstance();
            $object = $db->get("conf.service.example");
            $object->setAssoc($params);
            $db->set($object);
            // Return the configuration object.
            return $object->getAssoc();
        }
    }

    创建对应的配置文件
    在usr\share\openmediavault\datamodels创建conf.service.example.json

    {
        "type": "config",
        "id": "conf.service.example",
        "title": "EXAMPLE",
        "queryinfo": {
            "xpath": "//services/example",
            "iterable": false
        },
        "properties": {
            "enable": {
                "type": "boolean",
                "default": false
            },
            "max_value": {
                "type": "integer",
                "minimum": 1,
                "maximum": 100,
                "default": 0
            }
        }
    }

    在usr\share\openmediavault\datamodels创建rpc.example.json

    [{
        "type": "rpc",
        "id": "rpc.example.setsettings",
        "params": {
            "type": "object",
            "properties": {
                "enable": {
                    "type": "boolean",
                    "required": true
                },
                "max_value": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 100,
                    "required": true
                }
            }
        }
    }]

    运行命令重启omv服务:service openmediavault-engined restart

  5. 创建shell脚本获取配置信息
    创建执行脚本 example 执行命令:omv-mkconf example

    #!/bin/sh
    
    set -e
    
    . /etc/default/openmediavault
    . /usr/share/openmediavault/scripts/helper-functions
    
    OMV_EXAMPLE_XPATH="/config/services/example"
    OMV_EXAMPLE_CONF="/tmp/example.conf"
    
    cat <<EOF > ${OMV_EXAMPLE_CONF}
    enable    = $(omv_config_get "${OMV_EXAMPLE_XPATH}/enable")
    max_value = $(omv_config_get "${OMV_EXAMPLE_XPATH}/max_value")
    EOF
    
    exit 0

    为了监听触发执行脚本,将脚本放在/usr/share/openmediavault/mkconf目录下
    脚本权限改为 chmod 755 example

  6. 配置保存事件监听

    /usr/share/openmediavault/engined/module创建监听的example.inc

    <?php
    class OMVModuleExample extends \OMV\Engine\Module\ServiceAbstract
      implements \OMV\Engine\Notify\IListener {
        public function getName() {
            return "EXAMPLE";
        }
    
        public function applyConfig() {
            // 触发对应执行脚本
            $cmd = new \\OMV\\System\\Process("omv-mkconf", "example");
            $cmd->setRedirect2to1();
            $cmd->execute();
        }
    
        function bindListeners(\\OMV\\Engine\\Notify\\Dispatcher $dispatcher) {
            $dispatcher->addListener(OMV_NOTIFY_MODIFY,
                "org.openmediavault.conf.service.example",  // 和rpc内的配置文件一致conf.service.example
                [ $this, "setDirty" ]);
        }
    }

    运行命令重启omv服务:service openmediavault-engined restart

  7. 创建deb包 https://blog.csdn.net/gatieme...

改改心情
411 声望6 粉丝

温故知新,用时再找